<%--
  Created by IntelliJ IDEA.
  User: 29649
  Date: 2021/9/28
  Time: 20:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>人脸注册</title>
    <link rel="stylesheet" href="../../boot/plugin/layui/css/layui.css">
    <script type="text/javascript" src="../../boot/plugin/layui/layui.js"></script>
    <script type="text/javascript" src="http://47.100.102.177:8080/app/plugin/jquery.min.js"></script>
    <style type="text/css">
        body{
            background-image: url("http://47.100.102.177:8080/app/images/login/街的对面%20海边可爱女生插画4k壁纸_彼岸图网.jpg");
            background-size: cover;
        }
        .container{
            width: 420px;
            height: 520px;
            min-height: 520px;
            max-height: 320px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            padding: 20px;
            z-index: 130;
            border-radius: 8px;
            background-color: #fff;
            box-shadow: 0 3px 18px rgba(100, 0, 0, .5);
            font-size: 16px;
        }
        .close{
            background-color: white;
            border: none;
            font-size: 18px;
            margin-left: 410px;
            margin-top: -10px;
        }

        .layui-input{
            border-radius: 5px;
            width: 300px;
            height: 40px;
            font-size: 15px;
        }
        .layui-form-item{
            margin-left: -20px;
        }
        #logoid{
            margin-top: -16px;
            padding-left:150px;
            padding-bottom: 15px;
        }
        .layui-btn{
            margin-left: -30px;
            border-radius: 5px;
            font-size: 15px;
            top: 15px;
            position: relative;
        }
        a:hover{
            text-decoration: underline;
        }

        .media{
            position: relative;
            height: 300px;
        }
        #canvas{
            width:450px;
            height:300px;
            position: relative;
            left: 450px;
            top: -320px;
        }
        #video{
            width:450px;
            height:300px;
            position: relative;
            top: -10px;
            left: -15px;
        }
    </style>
</head>
<body>
<form class="layui-form" method="get">
    <div class="container">
        <button class="close" title="关闭">X</button>
        <div class="layui-form-mid layui-word-aux">
            <img id="logoid" src="http://47.100.102.177:8080/app/images/common/logo.png" style="width: 150px; height: auto;">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" id="username" name="uemail" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" id="uphone" name="uphone" required  lay-verify="required" placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="bton" class="layui-btn layui-btn-fluid" type="button">注册</button>
                </div>
            </div>
        </div>
        <div class="media">
            <video id="video" autoplay></video>
            <canvas id="canvas" ></canvas>
        </div>
        </div>
</form>
<script>
    let c=document.getElementsByClassName("close");
    let date=new Date();
    let datestr=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
    let btn=document.getElementById("bton");
    /**调用摄像头，获取媒体视频流**/
    var video = document.getElementById('video');
    //返回画布二维画图环境
    var userContext = canvas.getContext("2d");
    var getUserMedia =
        //浏览器兼容,表示在火狐、Google、IE等浏览器都可正常支持
        (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia)
    //getUserMedia.call(要调用的对象，约束条件，调用成功的函数，调用失败的函数)
    getUserMedia.call(navigator,{video: true,audio: false},function(localMediaStream){
        //获取摄像头捕捉的视频流
        video.srcObject=localMediaStream;
    },function(e){
        console.log("获取摄像头失败！！")
    });

    c.onclick=()=>{
        window.location.href = '/boot/index01';
    }

    btn.onclick=()=>{
        layer.load(1);
        var username = $("#username").val();
        var uphone = $("#uphone").val();
        if (username != null) {
            //点击按钮时拿到登陆者面部信息
            userContext.drawImage(video, 0, 0, 450, 300);
            var userImgSrc = document.getElementById("canvas").toDataURL("img/png");
            //拿到bash64格式的照片信息
            var faceBase = userImgSrc.split(",")[1];
            let parm={"faceBase": faceBase,
                "userName": username,
                "date": datestr,
                "phone": uphone}
            $.post("/boot/faceregister",parm,function (res) {
                if (res.code=== "200") {
                    alert("您的默认密码为："+res.data);
                    console.log(res.data);
                    window.location.href="facelogin";
                }
                if (res.code === "500") {
                    alert(res.msg);
                    window.location.href="facelogin";
                }
                else {
                   layer.msg(res.msg);
                   layer.closeAll();
                }
            })
        } else {
            alert("用户名不能为空");
        }
    }
</script>
</body>
</html>

